package com.future.controller;


import com.future.allUtils.utils.SecurityUtils;
import com.future.domain.DTO.*;
import com.future.domain.Result;
import com.future.service.UserService;
import com.future.service.FollowService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

@Api(tags = "用户模块")
@RestController("user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private FollowService followService;
    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    @GetMapping("getUserInfo")
    @ApiOperation("前端获取当前用户信息")
    public Result getUserInfo() {
        // 获取当前用户ID
        Long userId = SecurityUtils.getCurrentUser().getUser().getId();
        if (userId == null) {
            return Result.fail("请重新登录");
        }
        return userService.getUserInfo(userId);
    }

    @ApiOperation("按id获取其他用户信息")
    @GetMapping("/getOtherUserInfo/{id}")
    public Result getUserInfo(@PathVariable Long id) {
        return userService.getUserInfo(id);
    }

    @ApiOperation("注销账号")
    @PostMapping("/deleteAccount")
    public Result deleteAccount(@RequestBody UserLoginDTO dto) {
        return userService.deleteAccount(dto);
    }

    @ApiOperation("用户角色认证")
    @PostMapping("/roleCertification")
    public Result roleCertification(RoleCertificationDTO dto) {
        return userService.roleCertification(dto);
    }

    @ApiOperation("用户修改密码")
    @PostMapping("/updatePassword")
    public Result updatePassword(@RequestBody PasswordDTO dto) {
        return userService.updatePassword(dto);
    }

    @ApiOperation(
            value="点赞",
            notes = "只需要传targetId和targetTypeId"
    )
    @PostMapping("/like")
    public Result like(@RequestBody LikeDTO dto) {
        return userService.like(dto);
    }

    @ApiOperation(
            value="取消点赞",
            notes = "只需要传targetId和targetTypeId"
    )
    @PostMapping("/cancelLike")
    public Result cancelLike(@RequestBody LikeDTO dto) {
        return userService.cancelLike(dto);
    }

    @ApiOperation(
            value="获取用户点赞列表",
            notes = "只需要传类型targetTypeId"
    )
    @PostMapping("/getUserLikeList")
    public Result getUserLikeList(@RequestBody LikeDTO dto
                    ,@RequestParam(defaultValue = "1") Integer pageNum
                    ,@RequestParam(defaultValue = "10")Integer pageSize) {
        return userService.getUserLikeList(dto,pageNum,pageSize);
    }

    @ApiOperation(
            value="收藏",
            notes = "只需要传targetId和targetTypeId"
    )
    @PostMapping("/collection")
    public Result collection(@RequestBody CollectionDTO dto) {
        return userService.collection(dto);
    }

    @ApiOperation(
            value="取消收藏",
            notes = "只需要传targetId和targetTypeId"
    )
    @PostMapping("/cancelCollection")
    public Result cancelCollection(@RequestBody CollectionDTO dto) {
        return userService.cancelCollection(dto);
    }

    @ApiOperation(
            value="获取用户收藏列表",
            notes = "只需要传类型targetTypeId"
    )
    @PostMapping("/getUserCollectionList")
    public Result getUserCollectionList(@RequestBody CollectionDTO dto
            ,@RequestParam(defaultValue = "1") Integer pageNum
            ,@RequestParam(defaultValue = "10")Integer pageSize) {
        return userService.getUserCollectionList(dto,pageNum,pageSize);
    }

    @ApiOperation(value="学生申请加入班级")
    @PostMapping("/joinClass")
    public Result joinClass(@RequestBody StudentClassAuthenticationDTO dto) {
        return userService.joinClass(dto);
    }

    @ApiOperation("学生查询班级加入列表")
    @GetMapping("/getStudentClass")
    public Result getStudentClass(@RequestParam(defaultValue = "1") Integer pageNum,
                                  @RequestParam(defaultValue = "10") Integer pageSize) {
        return userService.getStudentClass(pageNum,pageSize);
    }


    @ApiOperation("学生取消加入班级申请")
    @DeleteMapping("/cancelJoinClass")
    public Result cancelJoinClass() {
        return userService.cancelJoinClass();
    }


    @ApiOperation("学生申请退出班级")
    @PostMapping("/quitClass")
    public Result quitClass(@RequestBody StudentClassAuthenticationDTO dto) {
        return userService.quitClass(dto);
    }
    @ApiOperation("更新用户信息")
    @PostMapping("/updateUser")
    public Result updateUser(@RequestBody UserDTO userDTO) {
        return userService.updateUser(userDTO);
    }

    @ApiOperation("获取用户收到的点赞、收藏、评论、关注、粉丝数")
    @GetMapping("/getTotalCollectionsReceived")
    public Result getTotalCollectionsReceived() {
        return userService.getTotalCountReceived(SecurityUtils.getCurrentUserId());
    }

    @ApiOperation("按类型检查是否被点赞收藏过")
    @PostMapping("/check/{targetTypeId}/{targetId}")
    public Result checkTargetType(@PathVariable Long targetId, @PathVariable Integer targetTypeId) {
        return userService.checkTargetType(targetId, targetTypeId);
    }
}
